﻿namespace UI
{
    using System;
    using System.Windows.Forms;

    using Common.ChartDrawing;
    using Common.EventArg;
    using Common.Helpers;
    using Common.Localization;

    public partial class NavigationPanel : UserControl, ILocalizable
    {
        public NavigationPanel()
        {
            this.InitializeComponent();

            this.scaleComboBox.SelectedIndex = 6;
        }

        #region public events

        public event EventHandler<DateTimeEventArgs> DateTimeChanged;

        public event EventHandler NavigateUp;

        public event EventHandler NavigateDown;

        public event EventHandler CenterChart;

        public event EventHandler<ChartScaleEventArgs> ScaleChanged;

        #endregion

        public void Localize()
        {
            this.timeLabel.Text = LocalizationProvider.GetMessage("time");
            this.dateLabel.Text = LocalizationProvider.GetMessage("date");
            this.navigateButton.Text = LocalizationProvider.GetMessage("navigate");
        }

        public void SetDateTime(DateTime dt)
        {
            this.datePicker.Value = dt;
            this.timePicker.Value = dt;
            Constants.CurrentTime = dt;
        }

        public DateTime GetCurrtenDateTime()
        {
            DateTime d = new DateTime(this.datePicker.Value.Year, this.datePicker.Value.Month,
                                      this.datePicker.Value.Day, this.timePicker.Value.Hour,
                                      this.timePicker.Value.Minute, this.timePicker.Value.Second);
            return d;
        }

        #region private functions

        private void NavigateButtonClick(object sender, EventArgs e)
        {
            if (this.DateTimeChanged != null)
            {
                this.DateTimeChanged(this, new DateTimeEventArgs(this.GetCurrtenDateTime()));
            }
        }

        private void UpNavigateClick(object sender, EventArgs e)
        {
            if (this.NavigateUp != null)
            {
                this.NavigateUp(this, null);
            }
        }

        private void DownNavigateClick(object sender, EventArgs e)
        {
            if (this.NavigateDown != null)
            {
                this.NavigateDown(this, null);
            }
        }

        private void CenterClick(object sender, EventArgs e)
        {
            if (this.CenterChart != null)
            {
                this.CenterChart(this, null);
            }
        }

        private void ScaleComboBoxItemChanged(object sender, EventArgs e)
        {
            if ( this.scaleComboBox.SelectedItem != "" && this.ScaleChanged != null)
            {
                this.ScaleChanged(this, new ChartScaleEventArgs(ChartScale.GetScale(this.scaleComboBox.SelectedIndex)));
            }
        }

        #endregion
    }
}